<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工地名称" prop="siteName">
        <el-input
          v-model="queryParams.siteName"
          placeholder="请输入工地名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['site:work:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['site:work:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workList"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工地名称" width="150"align="center" prop="siteName" />
      <el-table-column label="经度" width="100"align="center" prop="lng" />
      <el-table-column label="纬度" width="100"align="center" prop="lat" />
      <el-table-column label="法定代表人" width="100" align="center" prop="siteLegal" />
      <el-table-column label="企业负责人" width="100" align="center" prop="sitePrincipal" />
      <el-table-column label="企业负责人电话" width="120" align="center" prop="sitePrincipalTel" />
      <el-table-column label="安全负责人" width="100" align="center" prop="siteSafe" />
      <el-table-column label="安全负责人电话" width="120" align="center" prop="siteSafeTel" />
      <el-table-column label="安全值班电话" width="110" align="center" prop="siteSafeCell" />
      <el-table-column label="所属属地" width="100" align="center" prop="siteTerritory" />
      <el-table-column label="所属行业门类" width="100" align="center" prop="siteCategory" />
      <el-table-column label="省" width="100" align="center" prop="province" />
      <el-table-column label="市" width="100" align="center" prop="city" />
      <el-table-column label="区" width="100" align="center" prop="area" />
      <el-table-column label="街道" width="100" align="center" prop="street" />
      <el-table-column label="生产场所地址" width="100" align="center" prop="siteAddress" />
      <el-table-column label="操作" width="120" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工地基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="工地编号" prop="siteNumber">
          <el-input v-model="form.siteNumber" placeholder="请输入工地编号" />
        </el-form-item>
        <el-form-item label="工地名称" prop="siteName">
          <el-input v-model="form.siteName" placeholder="请输入工地名称" />
        </el-form-item>
        <el-form-item label="经度" prop="lng">
          <el-input v-model="form.lng" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="法定代表人" prop="siteLegal">
          <el-input v-model="form.siteLegal" placeholder="请输入法定代表人" />
        </el-form-item>
        <el-form-item label="企业负责人" prop="sitePrincipal">
          <el-input v-model="form.sitePrincipal" placeholder="请输入企业负责人" />
        </el-form-item>
        <el-form-item label="企业负责人电话" prop="sitePrincipalTel">
          <el-input v-model="form.sitePrincipalTel" placeholder="请输入企业负责人电话" />
        </el-form-item>
        <el-form-item label="安全负责人" prop="siteSafe">
          <el-input v-model="form.siteSafe" placeholder="请输入安全负责人" />
        </el-form-item>
        <el-form-item label="安全负责人电话" prop="siteSafeTel">
          <el-input v-model="form.siteSafeTel" placeholder="请输入安全负责人电话" />
        </el-form-item>
        <el-form-item label="安全值班电话" prop="siteSafeCell">
          <el-input v-model="form.siteSafeCell" placeholder="请输入安全值班电话" />
        </el-form-item>
        <el-form-item label="所属属地" prop="siteTerritory">
          <el-input v-model="form.siteTerritory" placeholder="请输入所属属地" />
        </el-form-item>
        <el-form-item label="所属行业门类" prop="siteCategory">
          <el-input v-model="form.siteCategory" placeholder="请输入所属行业门类" />
        </el-form-item>
        <el-form-item label="省" prop="province">
          <el-input v-model="form.province" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="区" prop="area">
          <el-input v-model="form.area" placeholder="请输入区" />
        </el-form-item>
        <el-form-item label="街道" prop="street">
          <el-input v-model="form.street" placeholder="请输入街道" />
        </el-form-item>
        <el-form-item label="生产场所地址" prop="siteAddress">
          <el-input v-model="form.siteAddress" placeholder="请输入生产场所地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWork, getWork, delWork, addWork, updateWork } from "@/api/system/work";

export default {
  name: "Work",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工地基础信息表格数据
      workList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        siteNumber: null,
        siteName: null,
        lng: null,
        lat: null,
        siteLegal: null,
        sitePrincipal: null,
        sitePrincipalTel: null,
        siteSafe: null,
        siteSafeTel: null,
        siteSafeCell: null,
        siteTerritory: null,
        siteCategory: null,
        province: null,
        city: null,
        area: null,
        street: null,
        siteAddress: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工地基础信息列表 */
    getList() {
      this.loading = true;
      listWork(this.queryParams).then(response => {
        this.workList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        siteNumber: null,
        siteName: null,
        lng: null,
        lat: null,
        siteLegal: null,
        sitePrincipal: null,
        sitePrincipalTel: null,
        siteSafe: null,
        siteSafeTel: null,
        siteSafeCell: null,
        siteTerritory: null,
        siteCategory: null,
        province: null,
        city: null,
        area: null,
        street: null,
        siteAddress: null,
        createTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工地基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWork(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工地基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWork(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWork(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工地基础信息编号为"' + ids + '"的数据项？').then(function() {
        return delWork(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('site/work/export', {
        ...this.queryParams
      }, `work_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
